package cn.neusoft.edu.his.demo.controller;

import io.swagger.annotations.Api;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.neusoft.edu.his.demo.common.Result;
import cn.neusoft.edu.his.demo.mapper.DepartmentMapper;
import cn.neusoft.edu.his.demo.po.Department;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

@CrossOrigin
@Api("DepartmentControlloer相关的api")
@RestController
public class DepartmentController {
	@Autowired
	private DepartmentMapper departmentMapper;


	@RequestMapping("/getDepartsLevel1")
	public Result getDepartsLevel1(){
		QueryWrapper<Department> wrapper=new QueryWrapper<Department>() ;
		 wrapper.eq("level",1);
		List<Department> list=departmentMapper.selectList(wrapper);
		return Result.ok(list);
	}
	@RequestMapping("/getAllDepartments")
	public Result getAllDepartments(){
		QueryWrapper<Department> wrapper1=new QueryWrapper<Department>() ;
		wrapper1.eq("level",1);
		List<Department> list1=departmentMapper.selectList(wrapper1);
		QueryWrapper<Department> wrapper2=new QueryWrapper<Department>() ;
		wrapper1.eq("level",2);
		List<Department> list2=departmentMapper.selectList(wrapper2);
		for(Department d1:list1){
			List<Department> children=new ArrayList<Department>();
			for(Department d2:list2)
			{
				if(d1.getDepartmentId()==d2.getParentId())
					children.add(d2);
			}
			d1.setChildren(children);
		}
		return Result.ok(list1);
	}
	
	@RequestMapping("/addDepartment")
	public void addDepartment(Department department){
		int insert = departmentMapper.insert(department);
	}

	@RequestMapping("/deleteDepartment")
	public void deleDepartment(Department department){
		int i = departmentMapper.deleteById(department.getDepartmentId());
	}

	@RequestMapping("/updateDepartment")
	public void updateDepartment(Department department){
		int i = departmentMapper.updateById(department);
	}

}

